import axios from 'axios'
import { Toast } from 'vant'
import { getToken } from '@/utils/storage'

// 创建axios实例
const request = axios.create({
  baseURL: 'http://liufusong.top:8080/',
  timeout: 5000 // 请求过时时间
})

// 添加请求拦截器
request.interceptors.request.use(function (config) {
  config.headers.authorization = getToken()
  return config
}, function (error) {
  return Promise.reject(error)
})

// 添加响应拦截器
request.interceptors.response.use(function (response) {
  return response.data
}, function (error) {
  if (error.response) {
    Toast.fail(error.response.data.message)
  }
  return Promise.reject(error)
})

export default request
